package com.wardrobe.smartWardrobe.config;

import com.wardrobe.smartWardrobe.mapper.MediaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebmvcConfig implements WebMvcConfigurer {
    @Qualifier("loginInterceptor")
    @Autowired
    private HandlerInterceptor loginInterceptor;

    @Qualifier("refreshTokenInterceptor")
    @Autowired
    private HandlerInterceptor refreshTokenInterceptor;

    //跨越处理器
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//项目中的所有接口都支持跨域
                .allowedOrigins("*")//所有地址都可以访问，也可以配置具体地址
                .allowCredentials(true)
                .allowedMethods("*")//"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"
                .maxAge(3600);// 跨域允许时间
    }

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //TODO:拦截器拦截地址需要修改
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns(
                        "/**"
                ).order(1);
        registry.addInterceptor(refreshTokenInterceptor).addPathPatterns("/**").order(0);
    }
}
